package com.hyts.bmsp.jfinal.processor;


import com.hyts.bmsp.jfinal.beans.WebMappingBean;
import com.hyts.bmsp.jfinal.beans.WebRouteBean;
import com.hyts.bmsp.jfinal.scanner01.JfinalClassScanner;
import org.apache.commons.lang.StringUtils;

import com.hyts.bmsp.jfinal.annotations.WebMapping;
import com.hyts.bmsp.jfinal.annotations.WebRoute;
import org.apache.log4j.Logger;

/**
 * @ClassName: AnnotationProcessor:注解处理器
 * @Description: 针对于WebMapping、WebRoute注解进行操作
 * @author: libo
 * @date: 2017年1月12日 上午10:42:13
 */
public final class JfinalClassProcessor {

	/**
	 * @fieldName: logger
	 * @fieldType: Logger
	 * @Description: 日志输出器
	 */
	private static final Logger logger = Logger.getLogger(JfinalClassProcessor.class.getName());
	
	/**
	 * @Title: processRoute
	 * @Description: 执行处理WebRoute Annotation处理器
	 * @param className
	 * @return: void
	 */
	public static void processRoute(String className){
		try {
			Class<? extends Object> clazz = Class.forName(className);
			WebRoute routeAnnotation = (WebRoute) clazz.getAnnotation(WebRoute.class);
			if(routeAnnotation == null && clazz.getSuperclass().getName().equals("Model")){
				logger.info("Class:[ "+className+" ]没有配置WebRoute注解！");
			}else{
				logger.info("【扫描注解Route-路由】->"+routeAnnotation.routeKey()+" ----> "+clazz.newInstance().getClass().getName());
				JfinalClassScanner.getRoutesList().add((new WebRouteBean(routeAnnotation.routeKey(),routeAnnotation.routeValue(),clazz.newInstance())));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @Title: processRoute
	 * @Description: 执行处理WebRoute Annotation处理器
	 * @param clazz
	 * @return: void
	 */
	public static void processRoute(Class<?> clazz){
		WebRoute routeAnnotation = (WebRoute) clazz.getAnnotation(WebRoute.class);
		if(routeAnnotation == null && clazz.getSuperclass().getName().equals("Model")){
			logger.info("Class:[ "+clazz.getName()+" ]没有配置WebRoute注解！");
		}else{
			try {
				logger.info("【扫描注解Route-路由】->"+routeAnnotation.routeKey()+" ----> "+clazz.newInstance().getClass().getName());
				JfinalClassScanner.getRoutesList().add((new WebRouteBean(routeAnnotation.routeKey(),routeAnnotation.routeValue(),clazz.newInstance())));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @Title: processMapping
	 * @Description: 执行处理WebMapping Annotation处理器
	 * @param className
	 * @return: void
	 */
	public static void processMapping(String className){
		try {
			Class<? extends Object> clazz = Class.forName(className);
			WebMapping routeAnnotation = (WebMapping) clazz.getAnnotation(WebMapping.class);
			if(routeAnnotation == null){
				logger.info("Class:[ "+className+" ]没有配置WebMapping注解！");
			}else{
				String tableName = routeAnnotation.tableName();
				String primaryKey = routeAnnotation.primaryKey();
				if(StringUtils.isEmpty(tableName) && clazz.getSuperclass().getName().equals("Model")){
					logger.info("tableName(表名称必须要进行标注)");
					return ;
				}else{
					logger.info("【扫描注解Mapping-路由】->"+routeAnnotation.tableName()+" ----> "+clazz.getName());
					JfinalClassScanner.getMappingsList().add((new WebMappingBean(routeAnnotation.tableName(),routeAnnotation.primaryKey(),clazz)));
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @Title: processMapping
	 * @Description: 执行处理WebMapping Annotation处理器
	 * @param clazz
	 * @return: void
	 */
	public static void processMapping(Class<?> clazz){
		WebMapping routeAnnotation = (WebMapping) clazz.getAnnotation(WebMapping.class);
			if(routeAnnotation == null){
				logger.info("Class:[ "+clazz.getName()+" ]没有配置WebMapping注解！");
			}else{
				String tableName = routeAnnotation.tableName();
				String primaryKey = routeAnnotation.primaryKey();
				if(StringUtils.isEmpty(tableName) && clazz.getSuperclass().getName().equals("Model")){
					logger.info("tableName(表名称必须要进行标注)");
					return ;
				}else{
					logger.info("【扫描注解Mapping-路由】->"+routeAnnotation.tableName()+" ----> "+clazz.getName());
					JfinalClassScanner.getMappingsList().add((new WebMappingBean(routeAnnotation.tableName(),routeAnnotation.primaryKey(),clazz)));
				}
			}
	}
	
}
